<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/2 0002
 * Time: 12:23
 */

namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\db;
use  think\session;

class Contestant extends AdminBase
{

    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     *   参赛选手
     *
     * */
    public function contestant($keyword = '', $page = 1){
        $sort = $this->request->param('cid');
        if($page != 1){
            if($keyword == null){
                $keyword = Session::get('keyword');
            }
        }
        $map = [];
        if ($keyword) {
            $map['playerName|playerMobile|openId'] = ['=', "{$keyword}"];
        }
        if($sort == 1){
            $sort = 'currentNum DESC';
        }else{
            $sort = 'createTime DESC';
        }
        $player_list = Db::table('vote_player')->where($map)->where(['status'=>1])->order($sort)->paginate(15, false, ['page' => $page]);
        return $this->fetch('contestant', ['player_list' => $player_list, 'keyword' => $keyword]);
    }

    /**
     *  票数调整
     *
     */
    public function update(){
        $id = $this->request->param('id');
        return $this->fetch('contestant/update',['id'=>$id]);
    }
    public function updateTicket(){
        $id = $this->request->param('id');
        $ticket = $this->request->param('ticket');
        $result = Db::table('vote_player')->where(['id'=>$id])->setInc('currentNum',$ticket);
        if ($result !== false) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    /**
     *  选手状态（锁定 / 未锁定）
     *
     * */
    public function status(){
        $id = $this->request->param('id');
        $isLock = $this->request->param('isLock');
        if($isLock == 0){// 锁定
            $result = Db::table('vote_player')->where(['id'=>$id])->update(['isLock'=>1]);
        }
        if($isLock == 1){ // 解锁
            $result = Db::table('vote_player')->where(['id'=>$id])->update(['isLock'=>0]);
        }
        if($result == 1){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    /**
     *  编辑
     *
     */
    public function edit(){
        $id = $this->request->param('id');
        $ticket = Db::table('vote_player')->where(['id'=>$id])->find();
        return $this->fetch('contestant/edit',['user'=>$ticket]);
    }
    public function editTicket(){
        // 手机号正则
        $id = $this->request->param('id');
        $playerName= $this->request->param('playerName');
        $playerMobile = $this->request->param('playerMobile');
//        $playerNum = $this->request->param('playerNum');
        if(!preg_match("/^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$/", $playerMobile,$match)){
            $this->success('手机号码格式不正确');
        }
        $result = Db::table('vote_player')->where(['id'=>$id])->update(['playerName'=>$playerName,'playerMobile'=>$playerMobile]);
        if ($result !== false) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    /**
     *  删除
     *
     **/
    public function delete(){
        $id = $this->request->param('id');
        $result = Db::table('vote_player')->where(['id'=>$id])->delete();
        if ($result > 0) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    /**
     * 删除图片
     *
     */
    public function deleteimg(){
        $imgurl = $this->request->param("imgurl");
        $id = $this->request->param('userid');
        $user = Db::table('vote_player')->where(['id' => $id])->find();
        $message = explode(",",$user['playerPic']);
        for($i = 0; $i < count($message); $i++){
            if($imgurl == $message[$i]){
                unset($message[$i]);
            }
        }
        $img = implode(',',$message);
        $result = Db::table('vote_player')->where(['id'=>$id])->update(['playerPic'=>$img]);
        if ($result > 0) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}